﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Refraction
{    
    public abstract class Actor :DrawableGameComponent
    {
        protected Game _game;
        protected Effect _effect;
        protected Model _model;
        public Vector3 Position;
        public static float IOR = 1.33f;
        public static float RefractionScale = 0.1f;

        public Actor(Game game, Vector3 position)  : base(game)
        {
            Position = position;
            _game = game;
        }

        public override void Update(GameTime gameTime)
        {
            KeyboardState currentKeyboardState = Keyboard.GetState();

            if (currentKeyboardState.IsKeyDown(Keys.Add))
            {
                IOR += 0.01f;
                RefractionScale += 0.001f;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Subtract))
            {
                IOR -= 0.01f;
                RefractionScale -= 0.001f;
            }

            IOR = MathHelper.Clamp(IOR, 1, 3);
            RefractionScale = MathHelper.Clamp(RefractionScale, 0, 2);

            base.Update(gameTime);
        }

        public override void  Draw(GameTime gameTime)
        {

 	         base.Draw(gameTime);
        }
        
        protected void DrawModel(Matrix world, Effect effect)
        {
            foreach (ModelMesh modelMesh in _model.Meshes)
            {
                foreach (ModelMeshPart modelMeshPart in modelMesh.MeshParts)
                {
                    effect.Parameters["World"].SetValue(world);

                    VertexBuffer vertexBuffer = modelMeshPart.VertexBuffer;
                    IndexBuffer indexBuffer = modelMeshPart.IndexBuffer;

                    _game.GraphicsDevice.SetVertexBuffer(vertexBuffer);
                    _game.GraphicsDevice.Indices = indexBuffer;

                    effect.Techniques[0].Passes[0].Apply();
                    _game.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, modelMeshPart.NumVertices, 0, modelMeshPart.PrimitiveCount);
                }
            } 
        }


        protected void DrawModel(Matrix world)
        {
            foreach (ModelMesh modelMesh in _model.Meshes)
            {
                foreach (ModelMeshPart modelMeshPart in modelMesh.MeshParts)
                {
                    _effect.Parameters["World"].SetValue(world);

                    VertexBuffer vertexBuffer = modelMeshPart.VertexBuffer;
                    IndexBuffer indexBuffer = modelMeshPart.IndexBuffer;

                    _game.GraphicsDevice.SetVertexBuffer(vertexBuffer);
                    _game.GraphicsDevice.Indices = indexBuffer;

                    _effect.Techniques[0].Passes[0].Apply();
                    _game.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, modelMeshPart.NumVertices, 0, modelMeshPart.PrimitiveCount);
                }
            }
        }


    }
}
